<?php 
require_once '../functions.php';
xiu_get_current_user();
require_once '../config.php';
header('Content-type:text/html');

function posts_add(){
  GLOBAL $message;
  GLOBAL $success;
  $success=false;
  $message='sss';
  $id=(int)$_SESSION['current_login_user']['id'];
    var_dump($id);
  if(empty($_POST['title'])){
    $message = '请输入文章标题';
    return ;
  } 

  if(empty($_POST['content'])){
    $message='请输入文章内容';
    return;
  }
 
  if(empty($_POST['slug'])){
    $message='请输入文章别名';
    return;
  }
  
  if(empty($_FILES['feature'])){
    $message='请上传图片';
    return;
  }
  if(empty($_FILES['feature'])){
    $message='请上传图片';
    return;
  }else{
    $file=$_FILES['feature']['tmp_name'];
    $filename=$_FILES['feature']['name'];
    $path='images/';
    $res=move_uploaded_file($file, $path.$filename);
    if($res){
      $success='上传成功';
    }else{
      $message='上传失败';
    }
  }
  
 
  if(empty($_POST['category'])){
    $message='请选择文章类型';
    return;
  }

  if(empty($_POST['created'])){
    $message='请选择上传时间';
    return;
  }

  if(empty($_POST['status'])){
    $message='请选择发表状态';
    return;
  }
  $null=null;
  $title=$_POST['title'];
  $slug=$_POST['slug'];
  $photo=$path.$filename;
  $category=(int)$_POST['category'];
  $created=$_POST['created'];
  $status=$_POST['status'];
  $content=$_POST['content'];
   $conn=mysqli_connect(XIU_DB_HOST,XIU_DB_USER,XIU_DB_PASS,XIU_DB_NAME);
    if(!$conn){
        $message='数据库连接失败';
        return;
    }
    $query=mysqli_query($conn,"insert into posts values (null,'{$slug}','{$title}','{$photo}','{$created}','{$content}',{$id},{$category},'{$status}')");
    if(!$query){
        //查询失败
      $message='插入失败';
        return false;
    }else{
     $success='发表成功';
    }

//不知道错那了的。。。。。。。。。。。。。。。。。。。。。。方法
  // $row=xiu_execute("insert into posts values (null,'{$slug}','{$title}','{$photo}','{$created}','{$content}',{$id},{$category},'{$status}');");
  // var_dump($row);
  // if(!$row){
  //   $message='数据插入失败';
   
  // }else{
  //    $success='cheng';
  // }
}

if($_SERVER['REQUEST_METHOD']=='POST'){
  posts_add();
}

$category=xiu_fetch_all("select * from categories");

?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <title>Add new post &laquo; Admin</title>
  <link rel="stylesheet" href="../static/assets/vendors/bootstrap/css/bootstrap.css">
  <link rel="stylesheet" href="../static/assets/vendors/font-awesome/css/font-awesome.css">
  <link rel="stylesheet" href="../static/assets/vendors/nprogress/nprogress.css">
  <link rel="stylesheet" href="../static/assets/css/admin.css">
  <script src="../static/assets/vendors/nprogress/nprogress.js"></script>
</head>
<body>
  <script>NProgress.start()</script>

  <div class="main">
    <?php include 'inc/navbar.php'; ?>
    <div class="container-fluid">
      <div class="page-title">
        <h1>写文章</h1>
      </div>
      <!-- 有错误信息时展示 -->
      <?php if (isset($message)): ?>
        <?php if ($success): ?>
          <div class="alert alert-success">
            <strong>成功！</strong><?php echo $success; ?>
          </div>
        
        <?php else: ?>
          <div class="alert alert-danger">
            <strong>错误！</strong><?php echo $message; ?>
          </div>
        <?php endif ?>
      <?php endif ?>
      <form class="row" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" autocomplete="off" enctype="multipart/form-data">
        <div class="col-md-9">
          <div class="form-group">
            <label for="title">标题</label>
            <input id="title" class="form-control input-lg" name="title" type="text" placeholder="文章标题">
          </div>
          <div class="form-group">
            <label for="content">内容</label>
            <textarea id="content" class="form-control input-lg" name="content" cols="30" rows="10" placeholder="内容"></textarea> 
            <!-- <script id="content" name="content" type="text/plain">这是初始值</script> --> 
          </div>
        </div>
        <div class="col-md-3">
          <div class="form-group">
            <label for="slug">别名</label>
            <input id="slug" class="form-control" name="slug" type="text" placeholder="slug">
            <p class="help-block">https://zce.me/post/<strong>slug</strong></p>
          </div>
          <div class="form-group">
            <label for="feature">特色图像</label>
            <!-- show when image chose -->
            <!-- <img class="help-block thumbnail" > -->
            <input id="feature" class="form-control" name="feature" type="file">
          </div>
          <div class="form-group">
            <label for="category">所属分类</label>
            <select id="category" class="form-control" name="category">
             <?php foreach ($category as $item): ?>
                <option value="<?php echo $item['id'] ?>"><?php echo $item['name'] ?></option>
             <?php endforeach ?>
             
            </select>
          </div>
          <div class="form-group">
            <label for="created">发布时间</label>
            <input id="created" class="form-control" name="created" type="datetime-local">
          </div>
          <div class="form-group">
            <label for="status">状态</label>
            <select id="status" class="form-control" name="status">
              <option value="drafted">草稿</option>
              <option value="published">已发布</option>
              <option value="trashed">回收站</option>
            </select>
          </div>
          <div class="form-group">
            <button class="btn btn-primary" type="submit">保存</button>
          </div>
        </div>
      </form>
    </div>
  </div>

  <?php $current_page = 'post-add'; ?>
  <?php include 'inc/sidebar.php'; ?>

  <script src="../static/assets/vendors/jquery/jquery.js"></script>
  <script src="../static/assets/vendors/bootstrap/js/bootstrap.js"></script>
  <!-- 两个文件顺序不能倒
  <script src="../static/assets/vendors/ueditor/ueditor.config.js"></script>
  <script src="../static/assets/vendors/ueditor/ueditor.all.js"></script> -->

  <script>
    // UE.getEditor('content', {
    //   initialFrameHeight: 1320,
    //   autoHeight: false
    // })

  </script>
  <script>NProgress.done()</script>
</body>
</html>
